/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Control;
import Model.ArrowDown;
import Model.ArrowLeft;
import Model.ArrowRight;
import Model.ArrowUp;
import Model.BlockColor;
import Model.Boundary;
import Model.Curve;
import Model.Diamond;
import Model.FillArrowDown;
import Model.FillArrowLeft;
import Model.FillArrowRight;
import Model.FillArrowUp;
import Model.FillDiamond;
import Model.FillHexagon;
import Model.FillOval;
import Model.FillRectangular;
import Model.FillRoundRectangular;
import Model.FillStar;
import Model.FillTriangle;
import Model.Hexagon;
import Model.Line;
import Model.Oval;
import Model.Point;
import Model.Rectangular;
import Model.RoundRectangular;
import Model.Shape;
import Model.Star;
import Model.Triangle;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.LinkedList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
/**
 *
 * @author PhucAnh
 */
 public class Control {
     int i=0;
    View view;
    LinkedList<Object> lLine1;
    LinkedList<Object> redo=new LinkedList<Object>();
    int nline=0;
    Point p;
    int kt=0;
    int x,y;
    Color oldColor=Color.BLACK;
//    View1 temp;
     public Control(View myview,LinkedList<Object> ll) {
//         this.temp=temp;
        this.view = myview;
        this.lLine1=ll;
        this.view.addViewMouseListener(new MouseAdapter() {
             
            @Override
            public void mouseClicked(MouseEvent e)
            { 
                 p=new Point(e.getX(),e.getY());
               if(view.getColorBlock()&&inCanvas(p.x, p.y))
                {
                    if(lLine1.size()>0&&view.getIsMove()==true ){
                view.setIsMove(false);
                lLine1.removeLast();
                }
                    view.redo.clear();
                    BufferedImage temp = new BufferedImage(view.getWidthPaint(),view.getHeightPaint(),java.awt.image.BufferedImage.TYPE_INT_RGB);
                Graphics g = temp.getGraphics();
                view.jpnPaint.paint(g);
                g.dispose();
                    Point[] ll=new Point[view.jpnPaint.getHeight()*view.jpnPaint.getWidth()];
                int n1=0;
                    int getColor = ((BufferedImage)temp).getRGB(p.x, p.y);
                    LinkedList<Point> Q = new LinkedList<Point>();
                    if(inCanvas(p.x, p.y) && ((BufferedImage)temp).getRGB(p.x, p.y) == getColor && getColor != view.getColor().getRGB()) {
                        Q.addLast(p);
                        while(!Q.isEmpty()) {
                        Point n = Q.removeLast();
                    if(inCanvas(n.x, n.y) && ((BufferedImage)temp).getRGB(n.x, n.y) == getColor) {
                 ll[n1]=n;
                 n1++;
                    ((BufferedImage)temp).setRGB(n.x, n.y, view.getColor().getRGB());
                    ((BufferedImage)view.jpnPaint.BufferImage).setRGB(n.x, n.y, view.getColor().getRGB());
                    Q.addLast(new Point(n.x-1, n.y));
                    Q.addLast(new Point(n.x+1, n.y));
                    Q.addLast(new Point(n.x, n.y+1));
                    Q.addLast(new Point(n.x, n.y-1));
                }
                        }
            }
                    Point[] ll1=new Point[n1];
               for(int i=0;i<n1;i++)
               {
                   ll1[i]=new Point();
                   ll1[i].x=ll[i].x;
                   ll1[i].y=ll[i].y;
               }
               ll=null;
               System.runFinalization();
                lLine1.addLast(new BlockColor(ll1,n1,view.getColor(),getColor));

//                view.jpnPaint.bufferGraphics.drawImage(view.jpnPaint.bufferImage, 0, 0, view.jpnPaint);
               }
               
//               else{                              //save animation-file
//                   writeToFile(lLine1);
//               }
               
//               else {                             //load animation-file
//                   lLine1.clear();
//                   lLine1=readFromFile(lLine1);
//                   view.jpnPaint.repaint();
//               }
               
               
            }
            
            public void mouseReleased(MouseEvent e)
            {
                if(lLine1.size()>=2)
                if(kt==1&&(!"CURVE".equals(view.getTypeShape())))
                   view.setIsMove(true);
                kt=0;
                if(view.isIsEraser())
                view.setColor(oldColor);
            }
           public void mousePressed(MouseEvent e) {
              if(lLine1.size()<2&&view.getIsMove()) view.setIsMove(false);
               if(lLine1.size()>=2)
               if( ( !(((Shape)lLine1.get(lLine1.size()-2)).isContain(new Point(e.getX(),e.getY()))) ) && view.getIsMove() ==true ){
               view.setIsMove(false);
               lLine1.removeLast();
               }
               if(view.isIsEraser())                     //eraser method
                {
                    view.setTypeShape("CURVE");
                    oldColor=view.getColor();

                    view.setColor(Color.WHITE);
                }
               p=new Point(e.getX(),e.getY());
             }
         });
        
        this.view.addViewMouseMotionListener(new MouseAdapter(){
            public void mouseDragged(java.awt.event.MouseEvent e)
            {
                

                switch(view.getTypeShape())
                {
                    case "OVAL":
                        if(view.getIsMove()==true){
                            ((Shape)lLine1.getLast()).toMove(e.getX()-p.getX(),e.getY()-p.getY());
                            ((Shape)lLine1.get(lLine1.size()-2)).toMove(e.getX()-p.getX(),e.getY()-p.getY());
                            p=new Point(e.getX(),e.getY());
                            break;
                        }
                        if(kt==0){
                            view.redo.clear();
                        lLine1.add(new Oval(p,new Point(e.getX(),e.getY()),view.getColor(),view.getBasicStroke()));
                        lLine1.add(new Boundary(p,new Point(e.getX(),e.getY())));
                        kt=1;}
                        else{
                        ((Oval)lLine1.get(lLine1.size()-2)).setPoint2(new Point(e.getX(),e.getY()));
                        ((Boundary)lLine1.getLast()).setPoint2(new Point(e.getX(),e.getY()));
                        }
                        break;
                    case "LINE":
                        if(view.getIsMove()==true){
                            ((Shape)lLine1.getLast()).toMove(e.getX()-p.getX(),e.getY()-p.getY());
                            ((Shape)lLine1.get(lLine1.size()-2)).toMove(e.getX()-p.getX(),e.getY()-p.getY());
                            p=new Point(e.getX(),e.getY());
                            break;
                        }
                        if(kt==0){view.redo.clear();
                        lLine1.add(new Line(p,new Point(e.getX(),e.getY()),view.getColor(),view.getBasicStroke()));
                        lLine1.add(new Boundary(p,new Point(e.getX(),e.getY())));
                        kt=1;}
                        else{
                        ((Line)lLine1.get(lLine1.size()-2)).setPoint2(new Point(e.getX(),e.getY()));
                        ((Boundary)lLine1.getLast()).setPoint2(new Point(e.getX(),e.getY()));
                        }
                        break;
                    case "RECTANGULAR":
                        if(view.getIsMove()==true){
                           ((Shape)lLine1.getLast()).toMove(e.getX()-p.getX(),e.getY()-p.getY());
                            ((Shape)lLine1.get(lLine1.size()-2)).toMove(e.getX()-p.getX(),e.getY()-p.getY());
                            p=new Point(e.getX(),e.getY());
                            break;
                        }
                        if(kt==0){view.redo.clear();
                        lLine1.add(new Rectangular(p,new Point(e.getX(),e.getY()),view.getColor(),view.getBasicStroke()));
                        lLine1.add(new Boundary(p,new Point(e.getX(),e.getY())));
                        kt=1;}
                        else{
                        ((Rectangular)lLine1.get(lLine1.size()-2)).setPoint2(new Point(e.getX(),e.getY()));
                        ((Boundary)lLine1.getLast()).setPoint2(new Point(e.getX(),e.getY()));
                        }
                        break;
                    case "TRIANGLE":
                        if(view.getIsMove()==true){
                           ((Shape)lLine1.getLast()).toMove(e.getX()-p.getX(),e.getY()-p.getY());
                            ((Shape)lLine1.get(lLine1.size()-2)).toMove(e.getX()-p.getX(),e.getY()-p.getY());
                            p=new Point(e.getX(),e.getY());
                            break;
                        }
                        if(kt==0){view.redo.clear();
                        lLine1.add(new Triangle(p,new Point(e.getX(),e.getY()),view.getColor(),view.getBasicStroke()));lLine1.add(new Boundary(p,new Point(e.getX(),e.getY())));
                        kt=1;}
                        else{
                        ((Triangle)lLine1.get(lLine1.size()-2)).setPoint2(new Point(e.getX(),e.getY()));
                        ((Boundary)lLine1.getLast()).setPoint2(new Point(e.getX(),e.getY()));
                        }
                        break;
                    case "ARROWL":
                        if(view.getIsMove()==true){
                            ((Shape)lLine1.getLast()).toMove(e.getX()-p.getX(),e.getY()-p.getY());
                            ((Shape)lLine1.get(lLine1.size()-2)).toMove(e.getX()-p.getX(),e.getY()-p.getY());
                            p=new Point(e.getX(),e.getY());
                            break;
                        }
                        if(kt==0){view.redo.clear();
                        lLine1.add(new ArrowLeft(p,new Point(e.getX(),e.getY()),view.getColor(),view.getBasicStroke()));lLine1.add(new Boundary(p,new Point(e.getX(),e.getY())));
                        kt=1;}
                        else{
                        ((ArrowLeft)lLine1.get(lLine1.size()-2)).setPoint2(new Point(e.getX(),e.getY()));
                        ((Boundary)lLine1.getLast()).setPoint2(new Point(e.getX(),e.getY()));
                        }
                        break;
                    case "ARROWR":
                        if(view.getIsMove()==true){
                            ((Shape)lLine1.getLast()).toMove(e.getX()-p.getX(),e.getY()-p.getY());
                            ((Shape)lLine1.get(lLine1.size()-2)).toMove(e.getX()-p.getX(),e.getY()-p.getY());
                            p=new Point(e.getX(),e.getY());
                            break;
                        }
                        if(kt==0){view.redo.clear();
                        lLine1.add(new ArrowRight(p,new Point(e.getX(),e.getY()),view.getColor(),view.getBasicStroke()));lLine1.add(new Boundary(p,new Point(e.getX(),e.getY())));
                        kt=1;}
                        else{
                        ((ArrowRight)lLine1.get(lLine1.size()-2)).setPoint2(new Point(e.getX(),e.getY()));
                        ((Boundary)lLine1.getLast()).setPoint2(new Point(e.getX(),e.getY()));
                        }
                        break;
                    case "ARROWU":
                        if(view.getIsMove()==true){
                            ((Shape)lLine1.getLast()).toMove(e.getX()-p.getX(),e.getY()-p.getY());
                            ((Shape)lLine1.get(lLine1.size()-2)).toMove(e.getX()-p.getX(),e.getY()-p.getY());
                            p=new Point(e.getX(),e.getY());
                            break;
                        }
                        if(kt==0){view.redo.clear();
                        lLine1.add(new ArrowUp(p,new Point(e.getX(),e.getY()),view.getColor(),view.getBasicStroke()));lLine1.add(new Boundary(p,new Point(e.getX(),e.getY())));
                        kt=1;}
                        else{
                        ((ArrowUp)lLine1.get(lLine1.size()-2)).setPoint2(new Point(e.getX(),e.getY()));
                        ((Boundary)lLine1.getLast()).setPoint2(new Point(e.getX(),e.getY()));
                        }
                        break;
                    case "ARROWD":
                        if(view.getIsMove()==true){
                            ((Shape)lLine1.getLast()).toMove(e.getX()-p.getX(),e.getY()-p.getY());
                            ((Shape)lLine1.get(lLine1.size()-2)).toMove(e.getX()-p.getX(),e.getY()-p.getY());
                            p=new Point(e.getX(),e.getY());
                            break;
                        }
                        if(kt==0){view.redo.clear();
                        lLine1.add(new ArrowDown(p,new Point(e.getX(),e.getY()),view.getColor(),view.getBasicStroke()));lLine1.add(new Boundary(p,new Point(e.getX(),e.getY())));
                        kt=1;}
                        else{
                        ((ArrowDown)lLine1.get(lLine1.size()-2)).setPoint2(new Point(e.getX(),e.getY()));
                        ((Boundary)lLine1.getLast()).setPoint2(new Point(e.getX(),e.getY()));
                        }
                        break;
                    case "STAR":
                        if(view.getIsMove()==true){
                            ((Shape)lLine1.getLast()).toMove(e.getX()-p.getX(),e.getY()-p.getY());
                            ((Shape)lLine1.get(lLine1.size()-2)).toMove(e.getX()-p.getX(),e.getY()-p.getY());
                            p=new Point(e.getX(),e.getY());
                            break;
                        }
                        if(kt==0){view.redo.clear();
                        lLine1.add(new Star(p,new Point(e.getX(),e.getY()),view.getColor(),view.getBasicStroke()));lLine1.add(new Boundary(p,new Point(e.getX(),e.getY())));
                        kt=1;}
                        else{
                        ((Star)lLine1.get(lLine1.size()-2)).setPoint2(new Point(e.getX(),e.getY()));
                        ((Boundary)lLine1.getLast()).setPoint2(new Point(e.getX(),e.getY()));
                        }
                        break;
                    case "DIAMOND":
                        if(view.getIsMove()==true){
                            ((Shape)lLine1.getLast()).toMove(e.getX()-p.getX(),e.getY()-p.getY());
                            ((Shape)lLine1.get(lLine1.size()-2)).toMove(e.getX()-p.getX(),e.getY()-p.getY());
                            p=new Point(e.getX(),e.getY());
                            break;
                        }
                        if(kt==0){view.redo.clear();
                        lLine1.add(new Diamond(p,new Point(e.getX(),e.getY()),view.getColor(),view.getBasicStroke()));lLine1.add(new Boundary(p,new Point(e.getX(),e.getY())));
                        kt=1;}
                        else{
                        ((Diamond)lLine1.get(lLine1.size()-2)).setPoint2(new Point(e.getX(),e.getY()));
                        ((Boundary)lLine1.getLast()).setPoint2(new Point(e.getX(),e.getY()));
                        }
                        break;
                    case "HEXAGON":
                        if(view.getIsMove()==true){
                            ((Shape)lLine1.getLast()).toMove(e.getX()-p.getX(),e.getY()-p.getY());
                            ((Shape)lLine1.get(lLine1.size()-2)).toMove(e.getX()-p.getX(),e.getY()-p.getY());
                            p=new Point(e.getX(),e.getY());
                            break;
                        }
                        if(kt==0){view.redo.clear();
                        lLine1.add(new Hexagon(p,new Point(e.getX(),e.getY()),view.getColor(),view.getBasicStroke()));lLine1.add(new Boundary(p,new Point(e.getX(),e.getY())));
                        kt=1;}
                        else{
                        ((Hexagon)lLine1.get(lLine1.size()-2)).setPoint2(new Point(e.getX(),e.getY()));
                        ((Boundary)lLine1.getLast()).setPoint2(new Point(e.getX(),e.getY()));
                        }
                        break;
                    case "ROUNDRECTAGULAR":
                        if(view.getIsMove()==true){
                            ((Shape)lLine1.getLast()).toMove(e.getX()-p.getX(),e.getY()-p.getY());
                            ((Shape)lLine1.get(lLine1.size()-2)).toMove(e.getX()-p.getX(),e.getY()-p.getY());
                            p=new Point(e.getX(),e.getY());
                            break;
                        }
                        if(kt==0){view.redo.clear();
                        lLine1.add(new RoundRectangular(p,new Point(e.getX(),e.getY()),view.getColor(),view.getBasicStroke()));lLine1.add(new Boundary(p,new Point(e.getX(),e.getY())));
                        kt=1;}
                        else{
                        ((RoundRectangular)lLine1.get(lLine1.size()-2)).setPoint2(new Point(e.getX(),e.getY()));
                        ((Boundary)lLine1.getLast()).setPoint2(new Point(e.getX(),e.getY()));
                        }
                        break;
                    case "FILLOVAL":
                        if(view.getIsMove()==true){
                            ((Shape)lLine1.getLast()).toMove(e.getX()-p.getX(),e.getY()-p.getY());
                            ((Shape)lLine1.get(lLine1.size()-2)).toMove(e.getX()-p.getX(),e.getY()-p.getY());
                            p=new Point(e.getX(),e.getY());
                            break;
                        }
                        if(kt==0){view.redo.clear();
                        lLine1.add(new FillOval(p,new Point(e.getX(),e.getY()),view.getColor(),view.getBasicStroke()));lLine1.add(new Boundary(p,new Point(e.getX(),e.getY())));
                        kt=1;}
                        else{
                        ((FillOval)lLine1.get(lLine1.size()-2)).setPoint2(new Point(e.getX(),e.getY()));
                        ((Boundary)lLine1.getLast()).setPoint2(new Point(e.getX(),e.getY()));
                        }
                        break;
                    case "FILLRECTANGULAR":
                        if(view.getIsMove()==true){
                            ((Shape)lLine1.getLast()).toMove(e.getX()-p.getX(),e.getY()-p.getY());
                            ((Shape)lLine1.get(lLine1.size()-2)).toMove(e.getX()-p.getX(),e.getY()-p.getY());
                            p=new Point(e.getX(),e.getY());
                            break;
                        }
                        if(kt==0){view.redo.clear();
                        lLine1.add(new FillRectangular(p,new Point(e.getX(),e.getY()),view.getColor(),view.getBasicStroke()));lLine1.add(new Boundary(p,new Point(e.getX(),e.getY())));
                        kt=1;}
                        else{
                        ((FillRectangular)lLine1.get(lLine1.size()-2)).setPoint2(new Point(e.getX(),e.getY()));
                        ((Boundary)lLine1.getLast()).setPoint2(new Point(e.getX(),e.getY()));
                        }
                        break;
                    case "FILLTRIANGLE":
                        if(view.getIsMove()==true){
                            ((Shape)lLine1.getLast()).toMove(e.getX()-p.getX(),e.getY()-p.getY());
                            ((Shape)lLine1.get(lLine1.size()-2)).toMove(e.getX()-p.getX(),e.getY()-p.getY());
                            p=new Point(e.getX(),e.getY());
                            break;
                        }
                        if(kt==0){view.redo.clear();
                        lLine1.add(new FillTriangle(p,new Point(e.getX(),e.getY()),view.getColor(),view.getBasicStroke()));lLine1.add(new Boundary(p,new Point(e.getX(),e.getY())));
                        kt=1;}
                        else{
                        ((FillTriangle)lLine1.get(lLine1.size()-2)).setPoint2(new Point(e.getX(),e.getY()));
                        ((Boundary)lLine1.getLast()).setPoint2(new Point(e.getX(),e.getY()));
                        }
                        break;
                    case "FILLARROWL":
                        if(view.getIsMove()==true){
                            ((Shape)lLine1.getLast()).toMove(e.getX()-p.getX(),e.getY()-p.getY());
                            ((Shape)lLine1.get(lLine1.size()-2)).toMove(e.getX()-p.getX(),e.getY()-p.getY());
                            p=new Point(e.getX(),e.getY());
                            break;
                        }
                        if(kt==0){view.redo.clear();
                        lLine1.add(new FillArrowLeft(p,new Point(e.getX(),e.getY()),view.getColor(),view.getBasicStroke()));lLine1.add(new Boundary(p,new Point(e.getX(),e.getY())));
                        kt=1;}
                        else{
                        ((FillArrowLeft)lLine1.get(lLine1.size()-2)).setPoint2(new Point(e.getX(),e.getY()));
                        ((Boundary)lLine1.getLast()).setPoint2(new Point(e.getX(),e.getY()));
                        }
                        break;
                    case "FILLARROWR":
                        if(view.getIsMove()==true){
                            ((Shape)lLine1.getLast()).toMove(e.getX()-p.getX(),e.getY()-p.getY());
                            ((Shape)lLine1.get(lLine1.size()-2)).toMove(e.getX()-p.getX(),e.getY()-p.getY());
                            p=new Point(e.getX(),e.getY());
                            break;
                        }
                        if(kt==0){view.redo.clear();
                        lLine1.add(new FillArrowRight(p,new Point(e.getX(),e.getY()),view.getColor(),view.getBasicStroke()));lLine1.add(new Boundary(p,new Point(e.getX(),e.getY())));
                        kt=1;}
                        else{
                        ((FillArrowRight)lLine1.get(lLine1.size()-2)).setPoint2(new Point(e.getX(),e.getY()));
                        ((Boundary)lLine1.getLast()).setPoint2(new Point(e.getX(),e.getY()));
                        }
                        break;
                    case "FILLARROWU":
                        if(view.getIsMove()==true){
                            ((Shape)lLine1.getLast()).toMove(e.getX()-p.getX(),e.getY()-p.getY());
                            ((Shape)lLine1.get(lLine1.size()-2)).toMove(e.getX()-p.getX(),e.getY()-p.getY());
                            p=new Point(e.getX(),e.getY());
                            break;
                        }
                        if(kt==0){view.redo.clear();
                        lLine1.add(new FillArrowUp(p,new Point(e.getX(),e.getY()),view.getColor(),view.getBasicStroke()));lLine1.add(new Boundary(p,new Point(e.getX(),e.getY())));
                        kt=1;}
                        else{
                        ((FillArrowUp)lLine1.get(lLine1.size()-2)).setPoint2(new Point(e.getX(),e.getY()));
                        ((Boundary)lLine1.getLast()).setPoint2(new Point(e.getX(),e.getY()));
                        }
                        break;
                    case "FILLARROWD":
                        if(view.getIsMove()==true){
                            ((Shape)lLine1.getLast()).toMove(e.getX()-p.getX(),e.getY()-p.getY());
                            ((Shape)lLine1.get(lLine1.size()-2)).toMove(e.getX()-p.getX(),e.getY()-p.getY());
                            p=new Point(e.getX(),e.getY());
                            break;
                        }
                        if(kt==0){view.redo.clear();
                        lLine1.add(new FillArrowDown(p,new Point(e.getX(),e.getY()),view.getColor(),view.getBasicStroke()));lLine1.add(new Boundary(p,new Point(e.getX(),e.getY())));
                        kt=1;}
                        else{
                        ((FillArrowDown)lLine1.get(lLine1.size()-2)).setPoint2(new Point(e.getX(),e.getY()));
                        ((Boundary)lLine1.getLast()).setPoint2(new Point(e.getX(),e.getY()));
                        }
                        break;
                    case "FILLSTAR":
                        if(view.getIsMove()==true){
                            ((Shape)lLine1.getLast()).toMove(e.getX()-p.getX(),e.getY()-p.getY());
                            ((Shape)lLine1.get(lLine1.size()-2)).toMove(e.getX()-p.getX(),e.getY()-p.getY());
                            p=new Point(e.getX(),e.getY());
                            break;
                        }
                        if(kt==0){view.redo.clear();
                        lLine1.add(new FillStar(p,new Point(e.getX(),e.getY()),view.getColor(),view.getBasicStroke()));lLine1.add(new Boundary(p,new Point(e.getX(),e.getY())));
                        kt=1;}
                        else{
                        ((FillStar)lLine1.get(lLine1.size()-2)).setPoint2(new Point(e.getX(),e.getY()));
                        ((Boundary)lLine1.getLast()).setPoint2(new Point(e.getX(),e.getY()));
                        }
                        break;
                    case "FILLDIAMOND":
                        if(view.getIsMove()==true){
                            ((Shape)lLine1.getLast()).toMove(e.getX()-p.getX(),e.getY()-p.getY());
                            ((Shape)lLine1.get(lLine1.size()-2)).toMove(e.getX()-p.getX(),e.getY()-p.getY());
                            p=new Point(e.getX(),e.getY());
                            break;
                        }
                        if(kt==0){view.redo.clear();
                        lLine1.add(new FillDiamond(p,new Point(e.getX(),e.getY()),view.getColor(),view.getBasicStroke()));lLine1.add(new Boundary(p,new Point(e.getX(),e.getY())));
                        kt=1;}
                        else{
                        ((FillDiamond)lLine1.get(lLine1.size()-2)).setPoint2(new Point(e.getX(),e.getY()));
                        ((Boundary)lLine1.getLast()).setPoint2(new Point(e.getX(),e.getY()));
                        }
                        break;
                    case "FILLHEXAGON":
                        if(view.getIsMove()==true){
                            ((Shape)lLine1.getLast()).toMove(e.getX()-p.getX(),e.getY()-p.getY());
                            ((Shape)lLine1.get(lLine1.size()-2)).toMove(e.getX()-p.getX(),e.getY()-p.getY());
                            p=new Point(e.getX(),e.getY());
                            break;
                        }
                        if(kt==0){view.redo.clear();
                        lLine1.add(new FillHexagon(p,new Point(e.getX(),e.getY()),view.getColor(),view.getBasicStroke()));lLine1.add(new Boundary(p,new Point(e.getX(),e.getY())));
                        kt=1;}
                        else{
                        ((FillHexagon)lLine1.get(lLine1.size()-2)).setPoint2(new Point(e.getX(),e.getY()));
                        ((Boundary)lLine1.getLast()).setPoint2(new Point(e.getX(),e.getY()));
                        }
                        break;
                    case "FILLROUNDRECTAGULAR":
                        if(view.getIsMove()==true){
                            ((Shape)lLine1.getLast()).toMove(e.getX()-p.getX(),e.getY()-p.getY());
                            ((Shape)lLine1.get(lLine1.size()-2)).toMove(e.getX()-p.getX(),e.getY()-p.getY());
                            p=new Point(e.getX(),e.getY());
                            break;
                        }
                        if(kt==0){view.redo.clear();
                        lLine1.add(new FillRoundRectangular(p,new Point(e.getX(),e.getY()),view.getColor(),view.getBasicStroke()));lLine1.add(new Boundary(p,new Point(e.getX(),e.getY())));
                        kt=1;}
                        else{
                        ((FillRoundRectangular)lLine1.get(lLine1.size()-2)).setPoint2(new Point(e.getX(),e.getY()));
                        ((Boundary)lLine1.getLast()).setPoint2(new Point(e.getX(),e.getY()));
                        }
                        break;
                   case "CURVE":
                        if(view.getIsMove()==true){
                            ((Shape)lLine1.getLast()).toMove(e.getX()-p.getX(),e.getY()-p.getY());
                            p=new Point(e.getX(),e.getY());
                            break;
                        }
                        if(kt==0){view.redo.clear();
                        lLine1.add(new Curve(view.getColor(),view.getBasicStroke()));kt=1;}
                            else
                        ((Curve)lLine1.getLast()).addPoint(new Point(e.getX(),e.getY()));
                        break;
                   default:
                       break;
                }
            }
        });
        
    }
     
     public boolean inCanvas(int x,int y)
     {
         
         if(view.jpnPaint.getSize().height>y&&x>=0&&view.jpnPaint.getSize().width>x&&y>=0)
             return true;
             return false;
     }
     
//     public void undo(BufferedImage a){
//         if(lLine1.size()>0){
//         Object tmp=lLine1.removeLast();
//         ((Shape)tmp).undo(a);
//         redo.addLast(tmp);
//         }
//     }
     
//     public void redo(BufferedImage a){
//         if(redo.size()>0){
//         Object tmp = redo.removeLast();
//         ((Shape)tmp).redo(a);
//         lLine1.addLast(tmp);
//         }
//     }
     
     public void writeToFile(LinkedList<Object> lLine1){  
  try {  
      //ghi hinh nen
      
      //
    FileOutputStream fos = new FileOutputStream ("keep.dat");  
    ObjectOutputStream oos = new ObjectOutputStream(fos); 
    oos.writeInt(lLine1.size());
    for(int i=0;i<lLine1.size();i++)
    oos.writeObject(lLine1.get(i));
    fos.close();  
  }   
  catch (Exception e) {  
    System.out.println(e);     
  }  
}
     
     public void writeImage(){
         BufferedImage temp = new BufferedImage(view.getWidthPaint(),view.getHeightPaint(),java.awt.image.BufferedImage.TYPE_INT_RGB);
         Graphics g = temp.getGraphics();
         view.jpnPaint.paint(g);
         g.dispose();
        try {
            ImageIO.write(temp, "png", new File("data.png"));
        } catch (IOException ex) {
            Logger.getLogger(Control.class.getName()).log(Level.SEVERE, null, ex);
        }
     }
     
     public void readImage(){
        try {
            BufferedImage img=ImageIO.read(new File("data.png"));
//            view.bufferGraphics.drawImage(img, 0, 0, );
//            view.bufferImage=(Image)img;
        } catch (IOException ex) {
            Logger.getLogger(Control.class.getName()).log(Level.SEVERE, null, ex);
        }
     }
     
     
     public LinkedList<Object> readFromFile(LinkedList<Object> lLine1){   
  try {  
      lLine1.clear();
      //nap hinh nen
      
      //
    FileInputStream fis = new  FileInputStream("keep.dat");  
    ObjectInputStream ois = new ObjectInputStream(fis); 
    Object obj;
    int size=ois.readInt();
    for(int i=0;i<size;i++){
    obj = (Shape)ois.readObject();
    lLine1.addLast(obj);
    }
    
  }   
  catch (Exception e) {  
    System.out.println(e);  
  }   
  return lLine1;  
}   
     
}
